package com.xn.app.muying;

import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jumper.chart.ADFetalHeartChart;
import com.jumper.chart.FHRAndTocoChartLineView;
import com.xn.app.muying.bean.ReXinLv;
import com.xn.app.muying.bean.ReXinLvAll;
import com.xn.app.muying.bean.ReXinLvXX;
import com.xn.app.muying.bean.XinLv;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.Constants;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.ToastUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Request;

public class TaiXinXIangXiActivity extends AppCompatActivity {
    private String TAG = this.getClass().getSimpleName();
    ADFetalHeartChart mAdFetalHeartChart;
    FHRAndTocoChartLineView chartView;
    TextView shi_chang, tai_xin, tai_dong,lu_zhi_shi_jian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tai_xin_xiang_xi);
        huoqushuju();
        shi_chang = (TextView) findViewById(R.id.shi_chang);
        tai_xin = (TextView) findViewById(R.id.tai_xin);
        tai_dong = (TextView) findViewById(R.id.tai_dong);
        lu_zhi_shi_jian = (TextView) findViewById(R.id.lu_zhi_shi_jian);
        findViewById(R.id.guan_bi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAdFetalHeartChart = (ADFetalHeartChart) findViewById(R.id.tai_xin_tu);
        if (mAdFetalHeartChart != null) {
            mAdFetalHeartChart.setHaveToco(false);
            // 得到一个真实的 胎心绘制View
            chartView = mAdFetalHeartChart.getLineView();
        }
//

    }


    AlertDialog zhuce_dialog;

    void huoqushuju() {
        zhuce_dialog = MyAlertDialog.ShowDialog(TaiXinXIangXiActivity.this, R.layout.dialog_loading);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String tokens = Constants.getStringByName(TaiXinXIangXiActivity.this, "yonghu");
                    Type type1 = new TypeToken<HashMap<String, String>>() {
                    }.getType();
                    HashMap<String, String> token = new Gson().fromJson(tokens, type1);
                    Log.i(TAG, "access_token==" + "Bearer " + token.get("access_token"));
                    Request request = new Request.Builder().url(APIMethod.HuoQuXinLvXiangXi + "/" + getIntent().getStringExtra("id")).addHeader("Authorization", "Bearer " + token.get("access_token")).get().build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();
                    int code = response.code();
                    String bodys = response.body().string();
                    Log.i(TAG, "code==" + code);
                    Log.i(TAG, bodys);
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    zhuce_dialog.dismiss();
                    Type type0 = new TypeToken<ReXinLvXX>() {
                    }.getType();
                    ReXinLvXX relmsg = new Gson().fromJson(msg.obj.toString(), type0);
                    shi_chang.setText(relmsg.getRecordSeconds());
                    tai_xin.setText(relmsg.getAvarageFHRs()+"");
                    tai_dong.setText(relmsg.getKeepScore()+"");
                    lu_zhi_shi_jian.setText("录制时间 ："+relmsg.getRecordDate().replace("T"," "));
                    for (int i : relmsg.getFhRs()) {
                        chartView.addData(i);
                    }
                    break;
                case 1:
                    zhuce_dialog.dismiss();
                    if (400 <= msg.arg1 && msg.arg1 < 500) {
                        ToastUtil.showToast(TaiXinXIangXiActivity.this, "修改失败");
                    } else {
                        Type type = new TypeToken<HashMap<String, Object>>() {
                        }.getType();
                        HashMap<String, Object> errmsg = new Gson().fromJson(msg.obj.toString(), type);
                        Type type2 = new TypeToken<HashMap<String, String>>() {
                        }.getType();
                        HashMap<String, String> error = new Gson().fromJson(errmsg.get("error").toString(), type2);
                        ToastUtil.showToast(TaiXinXIangXiActivity.this, error.get("message"));
                    }
                    break;
            }
        }
    };
}
